package com.ddm.controller;

import com.ddm.service.IBookDetailService;
import com.dto.AjaxResult;
import com.entity.BookDetail;
import com.entity.Employee;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by he on 2025/3/23 17:13
 */
@RestController
@RequestMapping("/admin/bookdetail")
public class BookDetailController {
    @Autowired
    public IBookDetailService bookDetailService;

    @PostMapping
    // 添加订单详情
    public AjaxResult addBookDetail(@RequestBody List<BookDetail> bookDetails){
        int rows =bookDetailService.insertBookDetail(bookDetails);
        return rows > 0 ? AjaxResult.success("新增订单详情成功") : AjaxResult.error("新增订单详情失败");
    }
    // 根据订单详情ID删除
    @DeleteMapping("/{bookdetailId}")
    public AjaxResult deleteBookDetailById(@PathVariable Integer bookdetailId) {
        int rows = bookDetailService.deleteBookDetailById(bookdetailId);
        return rows > 0 ? AjaxResult.success("删除订单详情成功") : AjaxResult.error("删除订单详情失败");
    }
    // 根据订单详情ID查询
    @GetMapping("/{bookdetailId}")
    public AjaxResult getBookDetailById(@PathVariable("bookdetailId") Integer bookDetailId) {
        BookDetail bookDetail = bookDetailService.selectBookDetailById(bookDetailId);
        return bookDetail != null ? AjaxResult.success(bookDetail) : AjaxResult.error("订单详情未找到");
    }
    // 分页查询订单详情
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@RequestParam(required = false)  BookDetail bookDetail) {
        PageHelper.startPage(pageNum, pageSize);
        List<BookDetail> list = bookDetailService.selectBookDetailList(bookDetail);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }

    // 根据订单ID分页查询订单详情
    @GetMapping("/page/{bookId}")
    public AjaxResult pageByBookId(@PathVariable("bookId") Long bookId, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        BookDetail queryParam = new BookDetail();
        queryParam.setBookId(Math.toIntExact(bookId));
        PageHelper.startPage(pageNum, pageSize);
        List<BookDetail> list = bookDetailService.selectBookDetailList(queryParam);
        PageInfo<BookDetail> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(pageInfo);
    }
}
